/*
 * @(#)UserDelegation.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ApplicationExceptionList;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.bean.SysFunction;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.UserErrorConstant;
import com.dcivision.user.bean.UserDelegation;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRole;
/**
  UserDelegationDAObject.java

  This class is the data access bean for table "USER_DELEGATION".

  @author      Rollo Chan
  @company     DCIVision Limited
  @creation date   07/09/2004
  @version     $Revision: 1.31 $
*/

public class UserDelegationDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.31 $";

  public static final String TABLE_NAME = "USER_DELEGATION";

  public UserDelegationDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("SOURCE_USER_ID");
    this.vecDBColumn.add("TARGET_OBJECT_TYPE");
    this.vecDBColumn.add("TARGET_OBJECT_ID");
    this.vecDBColumn.add("START_DATE");
    this.vecDBColumn.add("END_DATE");
    this.vecDBColumn.add("DELEGATION_RULE");
    this.vecDBColumn.add("OBJECT_TYPE");
    this.vecDBColumn.add("OBJECT_ID");
    this.vecDBColumn.add("NOTIFICATION_TYPE");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    return getByIDStatus(id, GlobalConstant.RECORD_STATUS_ACTIVE);
  }

  /**
   * @param id
   * @param status 
   * @return
   * @throws ApplicationException
   */
  public AbstractBaseObject getByIDStatus(Integer id, String status) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.ID = ?");
        if(!Utility.isEmpty(status)){
          sqlStat.append(" AND A.RECORD_STATUS = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        if(!Utility.isEmpty(status)){
          this.setPrepareStatement(preStat, 2, status);
        }
        
        rs = preStat.executeQuery();
        if (rs.next()) {
          UserDelegation tmpUserDelegation = new UserDelegation();
          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          return(tmpUserDelegation);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
	    PreparedStatement preStat = null;
	    PreparedStatement preStatCnt = null;
	    ResultSet rs = null;
	    ResultSet rsCnt = null;
	    StringBuffer sqlStat = new StringBuffer();
	    StringBuffer sqlStatCnt = new StringBuffer();
	    StringBuffer sqlStatTmp = new StringBuffer();
	    List result = new ArrayList();
	    int totalNumOfRecord = 0;
	    int rowLoopCnt = 0;
	    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
	    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
	    Integer sourceUserRecordID = sessionContainer.getUserRecordID();
	    String getFunctionName=null;
	    List getFunction=sessionContainer.getAccessibleSystemFunction();
	    java.util.Iterator iterator=getFunction.iterator();
	    while (iterator.hasNext())
	    {
	     SysFunction getSysFunction=(SysFunction)iterator.next();
	     if (SystemFunctionConstant.WORKFLOW_DELEGATION.equals(getSysFunction.getFunctionCode()))
	     {
	    	getFunctionName="hasdelegation"; 
	     }
	    }
	    synchronized(dbConn) {
	      try {
	        // join with user record
	        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
	          sqlStatTmp.append("select * from (SELECT A.ID, A.SOURCE_USER_ID, C.FULL_NAME, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID AS UD_UPDATER_ID, A.UPDATE_DATE AS UD_UPDATE_DATE, B.FULL_NAME AS TARGET_OBJECT_NAME ");
	        } else {
	          sqlStatTmp.append("SELECT A.ID, A.SOURCE_USER_ID, C.FULL_NAME, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID AS UD_UPDATER_ID, A.UPDATE_DATE AS UD_UPDATE_DATE, B.FULL_NAME AS TARGET_OBJECT_NAME ");
	        }

	        sqlStatTmp.append("FROM   USER_DELEGATION A, USER_RECORD B, USER_RECORD C ");
	        sqlStatTmp.append("WHERE  A.RECORD_STATUS = ? AND B.RECORD_STATUS = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_TYPE = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_ID = B.ID AND ");
	        sqlStatTmp.append("       A.SOURCE_USER_ID = C.ID ");
	        if (getFunctionName==null) {
	            sqlStatTmp.append("      AND A.SOURCE_USER_ID = ? ");
	        }
	        //sqlStatTmp.append("       ? >= A.START_DATE AND \n");
	        //sqlStatTmp.append("       (? < A.END_DATE OR A.END_DATE IS NULL) AND \n");
//	        sqlStatTmp.append("       A.SOURCE_USER_ID = ? ");

	        // Form the WHERE clause for filtering.
	        if (searchForm.isSearchable()) {
	          String searchField = searchForm.getBasicSearchField();
	          if ("TARGET_OBJECT_NAME".equals(searchField)) {
	            if (DataSourceFactory.DB_ORACLE == DataSourceFactory.getDatabaseType()) {
	              searchField = "UPPER(B.FULL_NAME)";
	            } else {
	              searchField = "B.FULL_NAME";
	            }
	          }else if ("SOURCE_USER_NAME".equals(searchField)) {
	            if (DataSourceFactory.DB_ORACLE == DataSourceFactory.getDatabaseType()) {
	              searchField = "UPPER(C.FULL_NAME)";
	            } else {
	              searchField = "C.FULL_NAME";
	            }
	          }
	          sqlStatTmp.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
	        }

	        //format the sql for any 'LIKE' statement contained
	        sqlStatTmp = this.getFormattedSQL(sqlStatTmp.toString());
	        sqlStat.append(sqlStatTmp);
	        sqlStatCnt.append(this.getSelectCountSQL(sqlStatTmp));

	        int i=1;
	        Timestamp currTime = Utility.getCurrentTimestamp();
	        Timestamp prevDay = Utility.addDay(currTime, -1);
	        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.SUBJECT_TYPE_USER);
	        if (getFunctionName==null) {
	            this.setPrepareStatement(preStatCnt, i++, sessionContainer.getUserRecordID());
	        }
	        //this.setPrepareStatement(preStatCnt, i++, currTime);
	        //this.setPrepareStatement(preStatCnt, i++, prevDay);
//	        this.setPrepareStatement(preStatCnt, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStatCnt, i++, searchKeyword);
	        }
	        rsCnt = preStatCnt.executeQuery();
	        if (rsCnt.next()) {
	          totalNumOfRecord += rsCnt.getInt(1);
	        }
	        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
	        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
	        sqlStatTmp = new StringBuffer();
	        sqlStatCnt = new StringBuffer();

	        // join with user role
	        sqlStatTmp.append("SELECT A.ID, A.SOURCE_USER_ID, C.FULL_NAME, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID AS UD_UPDATER_ID, A.UPDATE_DATE AS UD_UPDATE_DATE, B.ROLE_NAME AS TARGET_OBJECT_NAME ");
	        sqlStatTmp.append("FROM   USER_DELEGATION A, USER_ROLE B, USER_RECORD C ");
	        sqlStatTmp.append("WHERE  A.RECORD_STATUS = ? AND B.RECORD_STATUS = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_TYPE = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_ID = B.ID AND ");
	        sqlStatTmp.append("       A.SOURCE_USER_ID = C.ID ");
	        if (getFunctionName==null) {
	            sqlStatTmp.append("   AND    A.SOURCE_USER_ID = ? ");
	            }
	        //sqlStatTmp.append("       ? >= A.START_DATE AND \n");
	        //sqlStatTmp.append("       (? < A.END_DATE OR A.END_DATE IS NULL) AND \n");
//	        sqlStatTmp.append("       A.SOURCE_USER_ID = ? ");

	        // Form the WHERE clause for filtering.
	        if (searchForm.isSearchable()) {
	          String searchField = searchForm.getBasicSearchField();
	          if ("TARGET_OBJECT_NAME".equals(searchField)) {
	            searchField = getSearchColumn("ROLE_NAME","B");
	          } else if ("SOURCE_USER_NAME".equals(searchField)) {
	            searchField = getSearchColumn("FULL_NAME","C");
	          } else if (searchField.indexOf(".") < 0) {
	            searchField = getSearchColumn(searchField);
	          }
	          sqlStatTmp.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
	        }

	        //format the sql for any 'LIKE' statement contained
	        sqlStatTmp = this.getFormattedSQL(sqlStatTmp.toString());
	        sqlStat.append("UNION " + sqlStatTmp);
	        sqlStatCnt.append(this.getSelectCountSQL(sqlStatTmp));

	        i=1;
	        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.SUBJECT_TYPE_ROLE);
	        if (getFunctionName==null) {
	            this.setPrepareStatement(preStatCnt, i++, sessionContainer.getUserRecordID());
	            }
	        //this.setPrepareStatement(preStatCnt, i++, currTime);
	        //this.setPrepareStatement(preStatCnt, i++, prevDay);
//	        this.setPrepareStatement(preStatCnt, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStatCnt, i++, searchKeyword);
	        }
	        rsCnt = preStatCnt.executeQuery();
	        if (rsCnt.next()) {
	          totalNumOfRecord += rsCnt.getInt(1);
	        }
	        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
	        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
	        sqlStatTmp = new StringBuffer();
	        sqlStatCnt = new StringBuffer();

	        // join with user group
	        sqlStatTmp.append("SELECT A.ID, A.SOURCE_USER_ID, C.FULL_NAME, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID AS UD_UPDATER_ID, A.UPDATE_DATE AS UD_UPDATE_DATE, B.GROUP_NAME AS TARGET_OBJECT_NAME ");
	        sqlStatTmp.append("FROM   USER_DELEGATION A, USER_GROUP B, USER_RECORD C ");
	        sqlStatTmp.append("WHERE  A.RECORD_STATUS = ? AND B.RECORD_STATUS = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_TYPE = ? AND ");
	        sqlStatTmp.append("       A.TARGET_OBJECT_ID = B.ID AND ");
	        sqlStatTmp.append("       A.SOURCE_USER_ID = C.ID ");
	        if (getFunctionName==null) {
	            sqlStatTmp.append("     AND  A.SOURCE_USER_ID = ? ");
	            }
	        //sqlStatTmp.append("       ? >= A.START_DATE AND \n");
	        //sqlStatTmp.append("       (? < A.END_DATE OR A.END_DATE IS NULL) AND \n");
//	        sqlStatTmp.append("       A.SOURCE_USER_ID = ? ");

	        // Form the WHERE clause for filtering.
	        if (searchForm.isSearchable()) {
	          String searchField = searchForm.getBasicSearchField();
	          if ("TARGET_OBJECT_NAME".equals(searchField)) {
	            searchField = getSearchColumn("GROUP_NAME","B");
	          } else if ("SOURCE_USER_NAME".equals(searchField)) {
	            searchField = getSearchColumn("FULL_NAME","C");
	          } else if (searchField.indexOf(".") < 0) {
	            searchField = getSearchColumn(searchField);
	          }
	          sqlStatTmp.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
	        }

	        //format the sql for any 'LIKE' statement contained
	        sqlStatTmp = this.getFormattedSQL(sqlStatTmp.toString());
	        sqlStat.append("UNION " + sqlStatTmp);
	        sqlStatCnt.append(this.getSelectCountSQL(sqlStatTmp));
	        i=1;
	        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStatCnt, i++, GlobalConstant.SUBJECT_TYPE_GROUP);
	        if (getFunctionName==null){
	            this.setPrepareStatement(preStatCnt, i++, sessionContainer.getUserRecordID());
	            }
	        //this.setPrepareStatement(preStatCnt, i++, currTime);
	        //this.setPrepareStatement(preStatCnt, i++, prevDay);
//	        this.setPrepareStatement(preStatCnt, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStatCnt, i++, searchKeyword);
	        }
	        rsCnt = preStatCnt.executeQuery();
	        if (rsCnt.next()) {
	          totalNumOfRecord += rsCnt.getInt(1);
	        }
	        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
	        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

	        sqlStatTmp = new StringBuffer();
	        sqlStatCnt = new StringBuffer();
	        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE)
	          sqlStat.append(") DCI_UNIT_TABLE ");
	        // Form the ORDER clause for sorting.
	        if (searchForm.isSortable()) {
	          String sortAttribute = searchForm.getSortAttribute();
	          if (sortAttribute.indexOf(".") < 0 && !"TARGET_OBJECT_NAME".equals(sortAttribute)) {
	              sortAttribute = /*"A." + */sortAttribute;
	          }
	          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
	        }


	        // Get total number of record return.
	        //sqlStatCnt = this.getSelectCountSQL(sqlStat);

	        // Retrieve the result in row basis.
	        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        i=1;
	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.SUBJECT_TYPE_USER);
	        if (getFunctionName==null){
	            this.setPrepareStatement(preStat, i++, sessionContainer.getUserRecordID());
	            }
	        //this.setPrepareStatement(preStat, i++, currTime);
	        //this.setPrepareStatement(preStat, i++, currTime);
//	        this.setPrepareStatement(preStat, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStat, i++, searchKeyword);
	        }

	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.SUBJECT_TYPE_ROLE);
	        if (getFunctionName ==null) {
	            this.setPrepareStatement(preStat, i++, sessionContainer.getUserRecordID());
	            }
	        //this.setPrepareStatement(preStat, i++, currTime);
	        //this.setPrepareStatement(preStat, i++, currTime);
//	        this.setPrepareStatement(preStat, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStat, i++, searchKeyword);
	        }

	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, i++, GlobalConstant.SUBJECT_TYPE_GROUP);
	        if (getFunctionName==null) {
	            this.setPrepareStatement(preStat, i++, sessionContainer.getUserRecordID());
	            }
	        //this.setPrepareStatement(preStat, i++, currTime);
	        //this.setPrepareStatement(preStat, i++, currTime);
//	        this.setPrepareStatement(preStat, i++, sourceUserRecordID);
	        if (searchForm.isSearchable()) {
	          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
	          this.setPrepareStatement(preStat, i++, searchKeyword);
	        }
	        rs = preStat.executeQuery();

	        this.positionCursor(rs, startOffset, pageSize);

	        while (rs.next() && rowLoopCnt<pageSize) {
	          UserDelegation tmpUserDelegation = new UserDelegation();
	          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
	          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
	          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
	          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
	          tmpUserDelegation.setTargetObjectName(getResultSetString(rs, "TARGET_OBJECT_NAME"));
	          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
	          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
	          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
	          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
	          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
	          tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
	          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UD_UPDATER_ID"));
	          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UD_UPDATE_DATE"));
	          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
	          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
	          tmpUserDelegation.setRecordCount(totalNumOfRecord);
	          tmpUserDelegation.setRowNum(startOffset++);
	          ++rowLoopCnt;
	          result.add(tmpUserDelegation);
	        }
	        return(result);
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
	        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
	      }
	    }
	  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserDelegation tmpUserDelegation = new UserDelegation();
          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "NOTIFICATION_TYPE"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          result.add(tmpUserDelegation);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
    validateInsertOrUpdate(obj);
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
    validateInsertOrUpdate(obj);
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserDelegation tmpUserDelegation = (UserDelegation)((UserDelegation)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   USER_DELEGATION(ID, SOURCE_USER_ID, TARGET_OBJECT_TYPE, TARGET_OBJECT_ID, START_DATE, END_DATE, DELEGATION_RULE, OBJECT_TYPE, OBJECT_ID, NOTIFICATION_TYPE, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpUserDelegation.getSourceUserID());
        setPrepareStatement(preStat, 3, tmpUserDelegation.getTargetObjectType());
        setPrepareStatement(preStat, 4, tmpUserDelegation.getTargetObjectID());
        setPrepareStatement(preStat, 5, tmpUserDelegation.getStartDate());
        setPrepareStatement(preStat, 6, tmpUserDelegation.getEndDate());
        setPrepareStatement(preStat, 7, tmpUserDelegation.getDelegationRule());
        setPrepareStatement(preStat, 8, tmpUserDelegation.getObjectType());
        setPrepareStatement(preStat, 9, tmpUserDelegation.getObjectID());
        setPrepareStatement(preStat, 10, tmpUserDelegation.getNotificationType());
        setPrepareStatement(preStat, 11, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 12, new Integer(0));
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        setPrepareStatement(preStat, 15, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 16, currTime);
        preStat.executeUpdate();
        tmpUserDelegation.setID(nextID);
        tmpUserDelegation.setCreatorID(sessionContainer.getUserRecordID());
        tmpUserDelegation.setCreateDate(currTime);
        tmpUserDelegation.setUpdaterID(sessionContainer.getUserRecordID());
        tmpUserDelegation.setUpdateDate(currTime);
        tmpUserDelegation.setUpdateCount(new Integer(0));
        tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
        tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
        return(tmpUserDelegation);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserDelegation tmpUserDelegation = (UserDelegation)((UserDelegation)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE USER_DELEGATION ");
        sqlStat.append("SET  SOURCE_USER_ID=?, TARGET_OBJECT_TYPE=?, TARGET_OBJECT_ID=?, START_DATE=?, END_DATE=?, DELEGATION_RULE=?, OBJECT_TYPE=?, OBJECT_ID=?, NOTIFICATION_TYPE=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUserDelegation.getSourceUserID());
        setPrepareStatement(preStat, 2, tmpUserDelegation.getTargetObjectType());
        setPrepareStatement(preStat, 3, tmpUserDelegation.getTargetObjectID());
        setPrepareStatement(preStat, 4, tmpUserDelegation.getStartDate());
        setPrepareStatement(preStat, 5, tmpUserDelegation.getEndDate());
        setPrepareStatement(preStat, 6, tmpUserDelegation.getDelegationRule());
        setPrepareStatement(preStat, 7, tmpUserDelegation.getObjectType());
        setPrepareStatement(preStat, 8, tmpUserDelegation.getObjectID());
        setPrepareStatement(preStat, 9, tmpUserDelegation.getNotificationType());
        setPrepareStatement(preStat, 10, new Integer(tmpUserDelegation.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        setPrepareStatement(preStat, 13, tmpUserDelegation.getID());
        setPrepareStatement(preStat, 14, tmpUserDelegation.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpUserDelegation.setUpdaterID(sessionContainer.getUserRecordID());
          tmpUserDelegation.setUpdateDate(currTime);
          tmpUserDelegation.setUpdateCount(new Integer(tmpUserDelegation.getUpdateCount().intValue() + 1));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          return(tmpUserDelegation);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserDelegation tmpUserDelegation = (UserDelegation)((UserDelegation)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   USER_DELEGATION ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUserDelegation.getID());
        setPrepareStatement(preStat, 2, tmpUserDelegation.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpUserDelegation);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    UserDelegation tmpUserDelegation = (UserDelegation)this.oldValue;

    if (tmpUserDelegation != null) {
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getSourceUserID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getTargetObjectType()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getTargetObjectID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getStartDate()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getEndDate()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getDelegationRule()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getObjectType()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getObjectID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getNotificationType()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpUserDelegation.getUpdateDate()));
    }

    tmpUserDelegation = (UserDelegation)obj;
    if (tmpUserDelegation != null) {
      newValues.add(toAuditTrailValue(tmpUserDelegation.getID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getSourceUserID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getTargetObjectType()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getTargetObjectID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getStartDate()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getEndDate()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getDelegationRule()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getObjectType()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getObjectID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getNotificationType()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpUserDelegation.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized List getListBySourceUserID(Integer sourceUesrID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.SOURCE_USER_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, sourceUesrID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserDelegation tmpUserDelegation = new UserDelegation();
          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          result.add(tmpUserDelegation);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getListDelegatedToUser() throws ApplicationException {PreparedStatement preStat = null;
  		return this.getListDelegatedToUser(null);
  }

  public synchronized List getListDelegatedToUser(Integer targetObjectID) throws ApplicationException {PreparedStatement preStat = null;
	  ResultSet rs = null;
	  StringBuffer sqlStat = new StringBuffer();
	  List result = new ArrayList();
	
	  PermissionManager pm = sessionContainer.getPermissionManager();
	  List userRolesList = pm.getUserRoles();
	  List userGroupsList = pm.getUserGroups();
	
	  synchronized(dbConn) {
	    try {
	      sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, B.FULL_NAME, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, A.NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
	      sqlStat.append("FROM   USER_DELEGATION A, USER_RECORD B ");
	      sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.SOURCE_USER_ID = B.ID AND A.SOURCE_USER_ID <> ? AND ");
	      sqlStat.append("       ? >= A.START_DATE AND ");
	      sqlStat.append("       (? < A.END_DATE OR A.END_DATE IS NULL) AND ");
	      sqlStat.append("       ((A.TARGET_OBJECT_TYPE = '"+GlobalConstant.SUBJECT_TYPE_USER+"' AND A.TARGET_OBJECT_ID = ?) ");
	
	      String tmpStr="";
	      for (int i=0; userGroupsList!= null && i<userGroupsList.size(); i++) {
	        if (i!=0) {
	          tmpStr += ", ";
	        }
	        UserGroup group = (UserGroup) userGroupsList.get(i);
	        tmpStr +=  group.getID();
	      }
	      if (!Utility.isEmpty(tmpStr)) {
	        sqlStat.append("        OR (A.TARGET_OBJECT_TYPE = '"+GlobalConstant.SUBJECT_TYPE_GROUP+"' AND A.TARGET_OBJECT_ID in (" +tmpStr + ")) ");
	      }
	
	      tmpStr="";
	      for (int i=0; userRolesList != null && i<userRolesList.size(); i++) {
	        if (i!=0) {
	          tmpStr += ", ";
	        }
	        UserRole role = (UserRole) userRolesList.get(i);
	        tmpStr += role.getID();
	      }
	      if (!Utility.isEmpty(tmpStr)) {
	        sqlStat.append("        OR (A.TARGET_OBJECT_TYPE = '"+GlobalConstant.SUBJECT_TYPE_ROLE+"' AND A.TARGET_OBJECT_ID in (" +tmpStr + ")) ");
	      }
	      if(!Utility.isEmpty(targetObjectID)){
	    	  sqlStat.append("        OR (A.TARGET_OBJECT_TYPE = '"+GlobalConstant.SUBJECT_TYPE_GROUP+"') ");
	      }
	      sqlStat.append("        ) ");
	
	      Timestamp currTime = Utility.getCurrentTimestamp();
	      Timestamp prevDay = Utility.addDay(currTime, -1);
	      preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	      this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
	      this.setPrepareStatement(preStat, 2, sessionContainer.getUserRecordID());
	      this.setPrepareStatement(preStat, 3, currTime);
	      this.setPrepareStatement(preStat, 4, prevDay);
	      this.setPrepareStatement(preStat, 5, sessionContainer.getUserRecordID());
	
	      rs = preStat.executeQuery();
	      while (rs.next()) {
	        UserDelegation tmpUserDelegation = new UserDelegation();
	        tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
	        tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
	        tmpUserDelegation.setSourceUserName(getResultSetString(rs, "FULL_NAME"));
	        tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
	        tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
	        tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
	        tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
	        tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
	        tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
	        tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
	        tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
	        tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	        tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	        tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	        tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	        tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	        tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	        tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
	        tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
	        result.add(tmpUserDelegation);
	      }
	      return(result);
	    } catch (ApplicationException appEx) {
	      throw appEx;
	    } catch (SQLException sqle) {
	      log.error(sqle, sqle);
	      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	    } catch (Exception e) {
	      log.error(e, e);
	      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	    } finally {
	      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	    }
	  }
	}	
  
  private synchronized void validateInsertOrUpdate(AbstractBaseObject obj) throws ApplicationException {
    UserDelegation newDelegate = (UserDelegation) obj;
    ApplicationExceptionList exceptionList = new ApplicationExceptionList();

    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.SOURCE_USER_ID = ? AND ");
        sqlStat.append("       A.TARGET_OBJECT_TYPE = ? AND A.TARGET_OBJECT_ID = ? AND ");
        sqlStat.append("       A.OBJECT_TYPE = ? ");
        if (!Utility.isEmpty(newDelegate.getID()) ) {
          sqlStat.append("        AND A.ID != ? ");
        }
        //sqlStat.append("       AND A.OBJECT_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, newDelegate.getSourceUserID());
        this.setPrepareStatement(preStat, 3, newDelegate.getTargetObjectType());
        this.setPrepareStatement(preStat, 4, newDelegate.getTargetObjectID());
        this.setPrepareStatement(preStat, 5, newDelegate.getObjectType());
        if (!Utility.isEmpty(newDelegate.getID()) ) {
          this.setPrepareStatement(preStat, 6, newDelegate.getID());
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          Timestamp startDate = getResultSetTimestamp(rs, "START_DATE");
          Timestamp endDate = getResultSetTimestamp(rs, "END_DATE");

          if (startDate.compareTo(newDelegate.getStartDate())<=0 && (endDate==null || endDate.compareTo(newDelegate.getStartDate())>=0)) {
            exceptionList.addException(new ApplicationException(UserErrorConstant.EXIST_DELEGATION_IS_EFFECTIVE));
            break;
          } else if (newDelegate.getEndDate()!=null && startDate.compareTo(newDelegate.getEndDate())<=0 && (endDate==null || endDate.compareTo(newDelegate.getEndDate())>=0)) {
            exceptionList.addException(new ApplicationException(UserErrorConstant.EXIST_DELEGATION_IS_EFFECTIVE));
            break;
          }
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }

    exceptionList.throwException();
  }

  public synchronized List getListBySourceUserID(Integer sourceUesrID,java.util.Date currentDate) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.SOURCE_USER_ID = ? ");
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
        	sqlStat.append("AND ((A.START_DATE <= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS') ");
        	sqlStat.append("AND A.END_DATE >= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS')) OR ");
        	sqlStat.append("(A.START_DATE <= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS') ");
        	sqlStat.append("AND A.END_DATE IS NULL)) ");
        } else {
        	sqlStat.append("AND ((A.START_DATE <= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"' ");
        	sqlStat.append("AND A.END_DATE >= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"') OR ");
        	sqlStat.append("(A.START_DATE <= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"' ");
        	sqlStat.append("AND A.END_DATE IS NULL)) ");
        }
        log.debug("sqlStat: "+sqlStat);

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, sourceUesrID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserDelegation tmpUserDelegation = new UserDelegation();
          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          result.add(tmpUserDelegation);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  //get user list by sourceuserid and notification type
  public synchronized List getNotificationListBySourceUserID(Integer sourceUesrID,java.util.Date currentDate,String notificationType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SOURCE_USER_ID, A.TARGET_OBJECT_TYPE, A.TARGET_OBJECT_ID, A.START_DATE, A.END_DATE, A.DELEGATION_RULE, A.OBJECT_TYPE, A.OBJECT_ID, NOTIFICATION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_DELEGATION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.SOURCE_USER_ID = ? ");
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
            sqlStat.append("AND ((A.START_DATE <= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS') ");
            sqlStat.append("AND A.END_DATE >= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS')) OR ");
            sqlStat.append("(A.START_DATE <= TO_DATE('"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"', 'YYYY-MM-DD HH24:MI:SS') ");
            sqlStat.append("AND A.END_DATE IS NULL)) ");
            sqlStat.append("AND A.NOTIFICATION_TYPE LIKE '%"+notificationType+"%' ");
            
        } else {
            sqlStat.append("AND ((A.START_DATE <= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"' ");
            sqlStat.append("AND A.END_DATE >= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"') OR ");
            sqlStat.append("(A.START_DATE <= '"+TextUtility.formatDate(currentDate,"yyyy-MM-dd HH:mm:ss")+"' ");
            sqlStat.append("AND A.END_DATE IS NULL)) ");
            sqlStat.append("AND A.NOTIFICATION_TYPE LIKE '%"+notificationType+"%' ");
        }
        log.debug("sqlStat: "+sqlStat);

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, sourceUesrID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserDelegation tmpUserDelegation = new UserDelegation();
          tmpUserDelegation.setID(getResultSetInteger(rs, "ID"));
          tmpUserDelegation.setSourceUserID(getResultSetInteger(rs, "SOURCE_USER_ID"));
          tmpUserDelegation.setTargetObjectType(getResultSetString(rs, "TARGET_OBJECT_TYPE"));
          tmpUserDelegation.setTargetObjectID(getResultSetInteger(rs, "TARGET_OBJECT_ID"));
          tmpUserDelegation.setStartDate(getResultSetTimestamp(rs, "START_DATE"));
          tmpUserDelegation.setEndDate(getResultSetTimestamp(rs, "END_DATE"));
          tmpUserDelegation.setDelegationRule(getResultSetString(rs, "DELEGATION_RULE"));
          tmpUserDelegation.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserDelegation.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserDelegation.setNotificationType(getResultSetString(rs, "NOTIFICATION_TYPE"));
          tmpUserDelegation.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserDelegation.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserDelegation.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserDelegation.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserDelegation.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserDelegation.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserDelegation.setCreatorName(UserInfoFactory.getUserFullName(tmpUserDelegation.getCreatorID()));
          tmpUserDelegation.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserDelegation.getUpdaterID()));
          result.add(tmpUserDelegation);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
//king.guo----add----start--2006-04-13--
  public synchronized List getListByTargetObjectIDAndTargetObjectType(
			List objectIDList, String objectType) throws ApplicationException {
		PreparedStatement preStat = null;
		ResultSet rs = null;
		StringBuffer sqlStat = new StringBuffer();
		StringBuffer objectID = new StringBuffer();
		List result = new ArrayList();
		synchronized (dbConn) {
			try {
				if (objectIDList == null || objectIDList.size() == 0) {
					return null;
				}
				objectID.append(objectIDList.get(0));
				for (int i = 1; i < objectIDList.size(); i++) {
					objectID.append("," + objectIDList.get(i));
				}
				sqlStat
						.append("select ID from user_delegation where record_status=? and TARGET_OBJECT_TYPE=? and  TARGET_OBJECT_ID in("
								+ objectID.toString() + ")");
				log.debug("sqlStat: " + sqlStat);
				preStat = dbConn.prepareStatement(sqlStat.toString());
				this.setPrepareStatement(preStat, 1,
						GlobalConstant.RECORD_STATUS_ACTIVE);
				if (GlobalConstant.SUBJECT_TYPE_ROLE.equals(objectType)) {
					this.setPrepareStatement(preStat, 2,
							GlobalConstant.SUBJECT_TYPE_ROLE);
				} else if (GlobalConstant.SUBJECT_TYPE_USER.equals(objectType)) {
					this.setPrepareStatement(preStat, 2,
							GlobalConstant.SUBJECT_TYPE_USER);
				} else if (GlobalConstant.SUBJECT_TYPE_GROUP.equals(objectType)) {
					this.setPrepareStatement(preStat, 2,
							GlobalConstant.SUBJECT_TYPE_GROUP);
				}
				rs = preStat.executeQuery();
				while (rs.next()) {
					result.add(getResultSetInteger(rs, "ID"));
				}
				return result;
			} catch (ApplicationException e) {
				throw e;
			} catch (SQLException e) {
				log.error(e, e);
				throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR,
						e, e.toString());
			} catch (Exception e) {
				log.error(e, e);
				throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
			} finally {
				try {
					rs.close();
				} catch (Exception ignore) {
				} finally {
					rs = null;
				}
				try {
					preStat.close();
				} catch (Exception ignore) {
				} finally {
					preStat = null;
				}
			}
		}
	}
  // king.guo----add----end---2006-04-13--
  
}
